<template>
	<view>
		<!-- 搜索区域 -->
		<view class="search-box">
			<uni-search-bar @input="input" cancelButton="none" focus radius='15'></uni-search-bar>
		</view>

		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="kw">
			<view class="sugg-item" v-for="(item,i) in searchSuggestList" :key="i"
				@click="gotoFoodsDetail(item.goods_id)">
				<text>class="goods-name">{{item.goods_name}}</text>
				<uni-icons type="right" size="18"></uni-icons>
			</view>
		</view>

		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons size="18" type="trash" @click="clearHistoryList"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,i) in historyList" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				kw: '', // 搜索框内容
				searchSuggestList: [], // 搜索建议列表
				tiemr: null, // 记录定时器编号
				historyList: JSON.parse(uni.getStorageSync('kw') || "[]")
				// 搜索历史记录
			};
		},
		methods: {
			input(i) {
				// 清除 timer 对应的延时器
				clearTimeout(this.timer)
				// 重新启动一个延时器，并把赋值给 this.timer
				this.timer = setTimeout(() => {
					this.kw = i
					this.getsearchSuggestList()
				}, 1000)
			},
			async getsearchSuggestList() {
				if (this.kw.trim() === '') {
					return this.searchSuggestList = []
				}
				const {
					data
				} = await uni.$http.get('/goods/qsearch', {
					query: this.kw
				})
				if (data.meta.status != 200) return uni.$showToast()
				this.searchSuggestList = data.message

				this.saveHistory()
			},
			gotoFoodsDetail(goods_id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
				})
			},

			// 保存历史记录
			saveHistory() {
				this.historyList.unshift(this.kw)

				// Set:伪数组结构  是一个构造函数,使用必须用new关键字  
				// Set数据不会重复,添加数据时会进行去重  Set中两个空对象是不相等的
				const set = new Set(this.historyList)
				this.historyList = [...set]
				// 历史记录持久化处理
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			clearHistoryList() {
				this.historyList = []
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + item
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.uni-searchbar {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		position: relative;
		padding: 16rpx;
		/* 将默认的 #FFFFFF 改为 #C00000 */
		background-color: #c00000;
	}

	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.goods-name {
				// 文字不允许换行（单行文本）
				white-space: nowrap;
				// 溢出部分隐藏
				overflow: hidden;
				// 文本溢出后，使用 ... 代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
			}
		}
	}
</style>